package bp.ccfast;
import bp.da.AtPara;
import bp.da.DataRow;
import bp.da.DataTable;
import bp.da.DataType;
import bp.difference.handler.DirectoryPageBase;
import bp.sys.GenerDBSrc;
import bp.sys.GenerDBSrcs;
import bp.sys.GloGenerDBSrc;
import bp.sys.ResultObj;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.Hashtable;

public class Handler_CCFast extends DirectoryPageBase {

    public Handler_CCFast() {
    }
///#region GenerDBSrc 获得json.
    /**
     获得json数据.
     @return string 类型的 ResultObj
     */
    public final String GenerDBSrc_ByMyPK_WithAtPara() throws Exception {
        String atParas = this.GetRequestVal("MyParas");
        AtPara para = new AtPara(atParas);
        ResultObj obj = GloGenerDBSrc.GenerDBSrc_ResultObj(this.getMyPK(), para.getHisHT());

        if (obj.code != 200)
        {
            throw new RuntimeException("err@" + obj.message);
        }

        return obj.data.toString();
    }
    public final String GenerDBSrc_ByMyPK_WithJson() throws Exception {
        String atParas = this.GetRequestVal("MyParas");
        Hashtable<String, Object> ht = new Hashtable<>();

        if (atParas != null && !atParas.isEmpty()) {
            JSONObject json = JSONObject.fromObject(atParas);
            for (Object key : json.keySet()) {
                Object value = json.get(key);
                if (value == null || value.equals("null")) {
                    ht.put(key.toString(), "");
                } else {
                    ht.put(key.toString(), value);
                }
            }
        }
        ResultObj obj = GloGenerDBSrc.GenerDBSrc_ResultObj(this.getMyPK(), ht);

        if (obj.code != 200)
        {
            throw new RuntimeException("err@" + obj.message);
        }

        return obj.data.toString();
    }

    ///#endregion GenerDBSrc 获得json.
    //#region 报表获得数据.
    /// <summary>
    /// 初始化数据
    /// </summary>
    /// <returns>返回执行结果</returns>
    public final String RptWhite_Init() throws Exception {
        //1.定义容器.
        DataTable myTable = new DataTable();
        myTable.Columns.Add("WinNo"); //窗口编号
        myTable.Columns.Add("MarkID"); //数据源标记.
        myTable.Columns.Add("Code"); //代码 code=200 , 正常 500 错误。
        myTable.Columns.Add("Msg");
        myTable.Columns.Add("Data"); //返回的数据,一般都是json格式.

        //2.组织参数.
        String atParas = "@ND=" + this.GetRequestVal("ND") + "@RQFrom" + this.GetRequestVal("RQFrom") + "@RQTo=" + this.GetRequestVal("RQTo");
        atParas += "@NY=" + this.GetRequestVal("NY");
        atParas += "@JD=" + this.GetRequestVal("JD");
        AtPara myPara = new AtPara(atParas);

        //3.查询出来所有的数据源.
        GenerDBSrcs ens = new GenerDBSrcs();
        ens.Retrieve("RptID", this.getNo());

        //4. 查询数据.
        for (GenerDBSrc en : ens.ToJavaList())
        {
            ResultObj obj = GloGenerDBSrc.GenerDBSrc_ResultObj(en, myPara.getHisHT());
            DataRow dr = myTable.NewRow();
            dr.set("WinNo", en.getRptWindowsNo()); //窗口编号.
            dr.set("MarkID", en.getMarkID());
            dr.set("Code", obj.code);
            dr.set("Msg", obj.message);
            dr.set("Data", obj.data);
            myTable.Rows.add(dr);
        }
        //返回数据容器
        return bp.tools.Json.ToJson(myTable);
    }
    ///#endregion 报表获得数据.

    ///#region Init_GenerDBSrc()
    /**
     初始化数据.
     @return
     */
    public final String GenerDBSrc_InitData() throws Exception {
        //获取传递来的参数.
        String markID = this.GetRequestVal("MarkID");
        String dbModel = this.GetRequestVal("DBModel");
        String pkVal = this.GetRequestVal("PKVal");
        String doc = this.GetRequestVal("Doc");

        String doWay = this.GetRequestVal("DoWay");
        if (DataType.IsNullOrEmpty(doWay))
        {
            doWay = "";
        }

        String dbSrc = this.GetRequestVal("DBSrc");
        if (DataType.IsNullOrEmpty(dbSrc))
        {
            dbSrc = "local";
        }
        String objModel = this.GetRequestVal("ObjModel");

        GenerDBSrc en = GloGenerDBSrc.GenerDBSrc_InitData(markID, dbModel, pkVal);

        //属于单个属性的,都可以放入Doc 字段里.
        if (doWay.equals("SelfSQL") || doWay.equals("SFProc") || doWay.equals("BuessUnit") || doWay.equals("EventBase") || doWay.equals("StaticData") || doWay.equals("SearchDBSrc"))
        {
            en.SetValByKey("DBSrc", "local");
            en.SetValByKey("DoWay", doWay);
            en.SetValByKey("DBModel", dbModel);
            en.SetValByKey("DBSrc", dbSrc);
            en.SetValByKey("Doc", this.GetRequestVal("Doc")); //内容.
            en.Update();
        }

        // 接口模式.
        if (doWay.equals("SelfWebApiGet") || doWay.equals("SelfWebApiPost"))
        {
            en.SetValByKey("DBSrc", "local");
            en.SetValByKey("DoWay", doWay);
            en.SetValByKey("DBModel", dbModel);
            en.SetValByKey("DBSrc", dbSrc);

            en.SetValByKey("Doc", this.GetRequestVal("Doc")); //内容.
            en.SetValByKey("BodyDoc", this.GetRequestVal("BodyDoc"));
            en.SetValByKey("HeadDoc", this.GetRequestVal("HeadDoc"));
            en.Update();
        }
        return en.ToJson();
    }
    ///#endregion Init_GenerDBSrc()

}
